package cn.hexcloud.dga.governance.assessor;

import cn.hexcloud.dga.governance.bean.AssessParam;
import cn.hexcloud.dga.governance.bean.GovernanceAssessDetail;
import cn.hexcloud.dga.governance.bean.GovernanceMetric;
import cn.hexcloud.dga.meta.bean.TableMetaInfo;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang3.StringUtils;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
 * @author jiangdan7
 */
public abstract class Assessor {
    public final GovernanceAssessDetail metricAssess(AssessParam assessParam) {
        GovernanceAssessDetail governanceAssessDetail = new GovernanceAssessDetail();
        //1 获得元数据信息
        TableMetaInfo tableMetaInfo = assessParam.getTableMetaInfo();
        //2 获得指标信息
        GovernanceMetric governanceMetric = assessParam.getGovernanceMetric();
        //3  填写考评信息 （填写默认值的逻辑每个指标都一样)
        governanceAssessDetail.setAssessDate(assessParam.getAssessDate());
        governanceAssessDetail.setSchemaName(tableMetaInfo.getSchemaName());
        governanceAssessDetail.setTableName(tableMetaInfo.getTableName());
        governanceAssessDetail.setMetricId(governanceMetric.getId().toString());
        governanceAssessDetail.setMetricName(governanceMetric.getMetricName());
        governanceAssessDetail.setGovernanceType(governanceMetric.getGovernanceType());
        governanceAssessDetail.setTecOwner(tableMetaInfo.getTableMetaInfoExtra().getTecOwnerUserName());
        governanceAssessDetail.setAssessScore(BigDecimal.TEN);
        /* 4  判断得分逻辑  问题信息  url  每个指标不同 */
        try {
            checkProblem(governanceAssessDetail, assessParam);
        } catch (Exception e) {
            /* 5  处理考评时的异常   每个指标都一样 */
            governanceAssessDetail.setIsAssessException("1");
            StringWriter stringWriter = new StringWriter();
            PrintWriter printWriter = new PrintWriter(stringWriter);
            e.printStackTrace(printWriter);
            int maxLimit = Math.min(stringWriter.toString().length(), 2000);
            governanceAssessDetail.setAssessExceptionMsg(stringWriter.toString().substring(0, maxLimit));
        }

        //6   考评时间  每个指标都一样
        governanceAssessDetail.setCreateTime(new Date());
        /* 7   考评备注 */
        String metricDesc = governanceMetric.getMetricDesc();
        JSONObject paramJsonObj = JSON.parseObject(governanceMetric.getMetricParamsJson());
        if (paramJsonObj != null) {
            ArrayList<String> targets = new ArrayList<>();
            ArrayList<String> replacements = new ArrayList<>();
            paramJsonObj.forEach((k, v) -> {
                targets.add("{" + k + "}");
                replacements.add(v + "");
            });
            for (int i = 0; i < paramJsonObj.size(); i++) {
                metricDesc = metricDesc.replace(targets.get(i), replacements.get(i));
            }
        }
        governanceAssessDetail.setAssessComment(metricDesc);
        return governanceAssessDetail;

    }

    public abstract void checkProblem(GovernanceAssessDetail governanceAssessDetail, AssessParam assessParam) throws Exception;

}
